/**
 * SysDictIndexServiceImpl.java
 * Created at 2017-1-24
 * Created by guhuajie
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.system.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.common.utils.UUIDUtils;
import com.broadtext.rms.system.model.SysDictIndex;
import com.broadtext.rms.system.service.ISysDictIndexService;

/**
 * <p>
 * ClassName: SysDictIndexServiceImpl
 * </p>
 * <p>
 * Description: 字典索引表业务实现类
 * </p>
 * <p>
 * Author: guhuajie
 * </p>
 * <p>
 * Date: 2017-1-24
 * </p>
 */
@Service("sysDictIndexService")
public class SysDictIndexServiceImpl extends EntityServiceImpl<SysDictIndex> implements ISysDictIndexService {

    @Autowired
    @Qualifier("sysDictIndexDao")
    public void setEntityDao(EntityDao<SysDictIndex> entityDao) {
        super.entityDao = entityDao;
    }

    /**
     * 
     * <p>
     * Description: 更新字典索引
     * </p>
     * 
     * @param idArray dictindex主键数组
     * @param updateUser 更新人
     */
    public boolean updateDict(SysDictIndex record) throws ServiceException {
        try {
            SysDictIndex dictIndex;
            dictIndex = this.findEntityById(record.getPkId());
            boolean updateFlag;
            updateFlag = Boolean.TRUE;
            updateFlag = this.entityDao
                    .updateRecord("com.broadtext.rms.system.model.SysDictIndex.updateRecord", record);
            if (updateFlag) {
                //更新字典详情parentKey
                Map<String, Object> map;
                map = new HashMap<String, Object>();
                map.put("dictKey", dictIndex.getDictKey());
                map.put("dictKeyNew", record.getDictKey());
                updateFlag = this.entityDao.updateRecord(
                        "com.broadtext.rms.system.model.SysDictIndex.updateDictDetailByDictKey", map);
            }
            return updateFlag;
        } catch (DaoException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 
     * <p>
     * Description: 删除字典索引
     * </p>
     * 
     * @param idArray dictindex 主键数组
     * @param updateUser 更新人
     * @throws DaoException 异常
     * @throws ServiceException 异常
     */
    public void deleteDictIndex(String[] idArray, String updateUser) throws DaoException, ServiceException {
        for (String id : idArray) {
            SysDictIndex d;
            d = new SysDictIndex();
            d.setPkId(id);
            d.setUpdateDate(new Date());
            d.setUpdateUser(updateUser);
            d.setDelFlag("1");
            this.entityDao.updateRecord("com.broadtext.rms.system.model.SysDictIndex.updateRecord", d);
            //逻辑删除对应字典详情
            Map<String, Object> map;
            map = new HashMap<String, Object>();
            map.put("delFlag", "1");
            map.put("updateUser", updateUser);
            map.put("updateDate", new Date());
            map.put("parentKey", this.findEntityById(id).getDictKey());
            this.entityDao.updateRecord("com.broadtext.rms.system.model.SysDictIndex.updateDictDetailByDictKey", map);
        }
    }

    /**
     * 
     * <p>
     * Description: 根据parentKey获取字典详情
     * </p>
     * 
     * @param parentKey parentKey
     * @return 字典详情
     * @throws DaoException
     */
    @SuppressWarnings("unchecked")
    public List<Object> getDictByParentKey(String parentKey) throws DaoException {
        Map<String, Object> queryMap;
        queryMap = new HashMap<String, Object>();
        queryMap.put("parentKey", parentKey);
        return this.entityDao.findByQuery("com.broadtext.rms.system.model.SysDictIndex.getDictByParentKey", queryMap);
    }
    
    /**
     * 
     * <p>Description: 保存或编辑字典</p>
     * @param currentUser 当前登录人
     * @param dict 字典
     * @throws ServiceException 异常
     * @throws DaoException 异常
     */
    public Object saveOrUpdateDict(String currentUser, SysDictIndex dict) throws ServiceException, DaoException {
        String result = "SUCCESS";
        if (!"".equals(dict.getPkId())) {
            //编辑
            List<SysDictIndex> list;
            list = this.entityDao.findEntityByProperty("com.broadtext.rms.system.model.SysDictIndex.judgeUnique", dict);
            if(list.isEmpty()){
                dict.setUpdateUser(currentUser);
                dict.setUpdateDate(new Date());
                this.updateDict(dict);
            } else {
                result = "DUPLICATE";
            }
        } else {
            //新增
            List<SysDictIndex> list;
            list = this.entityDao.findEntityByProperty("com.broadtext.rms.system.model.SysDictIndex.judgeUnique", dict);
            if(list.isEmpty()){
            	dict.setPkId(UUIDUtils.getUUID());
                dict.setCreateUser(currentUser);
                dict.setCreateDate(new Date());
                dict.setUpdateUser(currentUser);
                dict.setUpdateDate(new Date());
                dict.setDelFlag("0");
                this.insertRecord(dict);
            } else {
                result = "DUPLICATE";
            }
            
        }
        return result;
    }
}
